<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>{{id?'编辑':'添加'}}签到信息</legend>
        </fieldset>
        <form class="layui-form" action="" lay-filter="example">

            <div class="layui-form-item">
                <label class="layui-form-label">包号</label>
                <div class="layui-input-block">
                    <input type="number" name="packageNo" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">签到奖励描述内容</label>
                <div class="layui-input-block">
                    <input type="text" name="qddescription" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">签到奖励开关</label>
                <div class="layui-input-block">
                    <input type="radio" name="isSign" lay-verify="required" class="layui-input" title="是"
                           :checked="(formData.isSign==1) ? true : false" value="1">
                    <input type="radio" name="isSign" lay-verify="required"  class="layui-input" title="否" value="0">
                </div>
                <div class="layui-form-mid">
                    游戏前端是否显示签到模块
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">公众号二维码链接</label>
                <div class="layui-input-block">
                    <input type="text" name="qdweixinbg" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">公众号名称</label>
                <div class="layui-input-block">
                    <input type="text" name="qdweixin" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" >
                <label class="layui-form-label">奖品列表</label>
                <button type="button"  class="layui-btn layui-btn-sm layui-btn-normal" v-on:click="listShowAdd()"><i class="fa fa-plus"></i>添加</button>
                <div class="layui-input-block">
                    <ul id="server-show">
                        <li class="layui-clear" v-for="(item,index) in rewardlist">
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input " v-model="item.id" />
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input " v-model="item.num" />
                            </div>
                            <div class="layui-input-inline">
                                <i class="fa fa-trash icon cursor del" v-on:click="rewardlist.splice(index,1)"></i>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>

    </div>
</div>
<style>

    #server-show li:nth-child(1){margin-top: 0;}
    #server-show li{
        margin-top:5px;
    }
    #server-show li .fa-trash{
        color:red;
    }
</style>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    var Id = urlParam('id')
    var form = null;
    new Vue({
        el:"#app",
        data:{
            id:Id,
            rewardlist:[],
            serverGroups:[],
            formData:{
                show:null,
                status:null,
                busy_status:null,
                pay_status:null,
                white_status:"0",
            }
        },
        updated:function(){
            form.render();
        },
        methods:{
            listShowAdd:function(){
                this.rewardlist.push({id:"",num:""})
            },
        },
        mounted:function() {

            var _this = this;
            layui.use(['form', 'laydate'], function () {
                form = layui.form;
                var layer = layui.layer, $ = layui.$,laydate=layui.laydate;
                laydate.render({
                    elem: '#enable_at',
                    type: 'datetime',
                    trigger: 'click'
                });
                //服务器分组
                $.get('/admin/serverGroup/paginate',{limit:100},function(res){
                    _this.serverGroups = res.data
                })
                if (Id) {
                    $.get('/admin/gameSign/' + Id, {}, function (res) {
                        var data = res.data;
                        _this.formData = data;
                        var waiting = setInterval(function () {
                           if (_this.serverGroups.length>0){
                               form.val('example', {
                                   "group_id": data.group_id,
                                   "server_id": data.server_id,
                                   "name": data.name,
                                   "push_url": data.push_url,
                                   "login_ip": data.login_ip,
                                   "login_port": data.login_port,
                                   "mysql_game_data_can_use": data.mysql_game_data_can_use,
                                   "can_push": data.can_push,
                                   "show": data.show,
                                   "status": data.status,
                                   "busy_status": data.busy_status,
                                   "pay_status": data.pay_status,
                                   "enable_at": data.enable_at,
                               })
                               clearInterval(waiting)
                           }
                        },1)
                    })
                    //表单初始赋值
                }
                //监听提交
                form.on('submit(demo1)', function (data) {
                    console.log('奖品列表',_this.rewardlist)
                    var formData = {
                        ...data.field,
                        rewardlist: JSON.stringify(_this.rewardlist.map(item => ({ id: parseInt(item.id), num: parseInt(item.num) })))
                    };
                    if (Id) {

                        formData._method = 'put';
                        $.post('/admin/gameSign/' + Id, formData, function (res) {
                            if (res.code === 0) {
                                layer.msg('success', {icon: 6, time: 500}, function () {
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        })
                    } else { //add

                        $.post('/admin/gameSign/add', formData, function (res) {
                            if (res.code === 0) {
                                layer.msg('success', {icon: 6, time: 500}, function () {
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        })
                    }

                    return false;
                });
                form.on('radio(status)',function(data){
                    _this.formData.status = data.value;
                })
                form.render();
            })
        }
    });
</script>

</body>
</html>